package org.openoa.antreport.config;

import com.bstek.ureport.definition.datasource.BuildinDatasource;
import com.bstek.ureport.definition.datasource.DataSourceFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
 *
 * @className: UreportDataSource
 * @author: Tyler Zhou
 * @since: 2025-07-13
 **/
@Slf4j
@Configuration
public class AntReportDataSource implements BuildinDatasource {
    @Autowired
    private DataSource dataSource;
    @Autowired
    private DataSourceFactory dataSourceFactory;

    @Override
    public String name() {
        return "antReportDatasource";
    }

    @Override
    public Connection getConnection(){
        try {
            if(dataSource==null){
                dataSource=dataSourceFactory.createAndGetDatasource();
            }
            return dataSource.getConnection();
        } catch (SQLException e) {
            log.error("AntReport 获取数据连接失败");
        }
        return null;
    }
}
